using Config;
using Data;
using Entity;
using Entity.Player.State;
using MoonFramework.FSM;
using MoonFramework.Scripts.Event;
using MoonFramework.Scripts.Tool;
using MoonFramework.Template;
using Tool.Name;
using UnityEngine;

namespace Weapon
{
    public class WeaponController
    {
        public BaseWeapon CurrentWeapon { get; private set; } = new();
        
        public void ChangeWeapon(ItemData newWeaponData, Character character)
        {
            if (newWeaponData == CurrentWeapon.WeaponData)
                return;
            
            if (CurrentWeapon.WeaponData != null)
                CurrentWeapon.WeaponObject.MoonGameObjPushPool();
            if (newWeaponData != null)
            { 
                ItemWeaponInfo newWeaponInfo = (ItemWeaponInfo)newWeaponData.Config.ItemTypeInfo;
                CurrentWeapon.SetWeaponObject(GameObjPoolManager.Instance.Pop<GameObject>(newWeaponInfo.PrefabOnActor.name, newWeaponInfo.PrefabOnActor));
                CurrentWeapon.WeaponObject.transform.SetParent(character.WeaponPoint);
                CurrentWeapon.WeaponObject.transform.localPosition = newWeaponInfo.Position;
                CurrentWeapon.WeaponObject.transform.localRotation = Quaternion.Euler(newWeaponInfo.Rotation);
                character.viewController._AttackAnimation = newWeaponInfo.WeaponAnimation;
                character.viewController._IdleAnimation = newWeaponInfo.IdleAnimation;
            }
            else
            {
                character.viewController._AttackAnimation = character.AnimationConfig.PickUp;
                character.viewController._IdleAnimation = character.AnimationConfig.Idle;
            }
            character.fsmMachine.ChangeState(nameof(PlayerIdleState), true);
            CurrentWeapon?.SetWeaponData(newWeaponData);
        }

        public void WeaponDamaged()
        {
            EventCenter.Instance.Callback(EventName.WeaponAttackSucceed);
        }
    }
}